package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.ProhibitAreaDao;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.ProhibitArea;
import com.tbit.main.service.DataService;
import com.tbit.main.service.ProhibitAreaService;
import com.tbit.main.util.GeoUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("prohibitAreaService")
public class ProhibitAreaServiceImpl implements ProhibitAreaService {
    @Autowired
    private ProhibitAreaDao prohibitAreaDao;
    @Autowired
    private DataService dataService;

    @Override
    public void insert(ProhibitArea prohibitArea) {
        prohibitAreaDao.insert(prohibitArea);
    }

    @Override
    public void update(ProhibitArea prohibitArea) {
        prohibitAreaDao.update(prohibitArea);
    }

    @Override
    public void del(Integer prohibitAreaId) {
        prohibitAreaDao.del(prohibitAreaId);
    }

    @Override
    public ProhibitArea getByProhibitAreaId(Integer prohibitAreaId) {
        return prohibitAreaDao.getByProhibitAreaId(prohibitAreaId);
    }

    @Override
    public List<ProhibitArea> getByAccountId(Integer accountId) {
        return prohibitAreaDao.getByAccountId(accountId);
    }

    @Override
    public List<ProhibitArea> getByRegionId(List<Integer> regionId) {
        return prohibitAreaDao.getByRegionId(StringUtil.getListString(regionId));
    }

    @Override
    public List<ProhibitArea> getByAccountIds(List<Integer> accountIds, String updateTime) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountIds, StringUtil.getListString(accountIds));
        map.put("updateTime", updateTime);
        return prohibitAreaDao.getByAccountIds(map);
    }

    @Override
    public List<ProhibitArea> getAccountIds(List<Integer> accountIds) {
        return prohibitAreaDao.getAccountIds(StringUtil.getListString(accountIds));
    }

    @Override
    public void updateBatch(List<Map<String, Object>> maps) {

        List<List<Map<String, Object>>> prhoibitGeo = ListUtils.partition(maps, 900);
        for (List<Map<String, Object>> prhoibit : prhoibitGeo) {
            prohibitAreaDao.updateBatch(prhoibit);
        }
    }

    @Override
    public List<ProhibitArea> getByAccountAndArea(Integer accountId, Double lo, Double la, Integer radius) {
        Double area = 0.00001D * radius;

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("minLo", lo - area);
        params.put("maxLo", lo + area);
        params.put("minLa", la - area);
        params.put("maxLa", la + area);

        return prohibitAreaDao.getByAccountAndArea(params);
    }

    @Override
    public List<ProhibitArea> getByRegionAndArea(List<Integer> regionId, Double lo, Double la, Integer radius) {
        Double area = 0.00001D * radius;

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("minLo", lo - area);
        params.put("maxLo", lo + area);
        params.put("minLa", la - area);
        params.put("maxLa", la + area);

        return prohibitAreaDao.getByRegionAndArea(params);
    }

    @Override
    public ProhibitArea checkInProhibitArea(Integer accountId, Point point, Integer radius) {
        ProhibitArea prohibitArea = null;

        List<ProhibitArea> prohibitAreas = getByAccountAndArea(accountId, point.getX(), point.getY(), radius);

        /** 判断车辆是否在禁停区内 */
        for (ProhibitArea pa : prohibitAreas) {
            Double distince = null;

            List<Point> points = GeoUtil.getPoints(pa.getPoints());

            /**判断是否在多边形内*/
            if (GeoUtil.IsPtInPoly(point, points)) {
                /**距离边界的距离大于允许的距离*/
                distince = GeoUtil.getDistance(point, points);
                if (distince > pa.getAllowRange()) {
                    prohibitArea = pa;
                    break;
                }
            }

        }

        return prohibitArea;
    }

    @Override
    public List<ProhibitArea> getAll() {
        return prohibitAreaDao.getAll();
    }

    @Override
    public List<Integer> getNotRegionId() {
        return prohibitAreaDao.getNotRegionId();
    }

    @Override
    public void delRegionId(List<Integer> prohibitAreaIds) {
        List<List<Integer>> prohibitAreaGeo = ListUtils.partition(prohibitAreaIds, 900);
        for (List<Integer> prohibitAreaId : prohibitAreaGeo) {
            prohibitAreaDao.delRegionId(StringUtil.getListString(prohibitAreaId));
        }
    }

    @Override
    public ProhibitArea getByProhibitAreaName(Integer accountId, String prohibitAreaName) {
        return prohibitAreaDao.getByProhibitAreaName(accountId, prohibitAreaName);
    }
}